package com.jason.java.singleton;

/**
 * @Author: Jason
 * @Date: 2017/7/23
 * @Time: 20:22
 */
public class LazyThreadSafeDoubleCheck {

    // volatile 保证了构造方法和赋值 INSTANCE = new LazyThreadSafeDoubleCheck(); 的调用是有序的
    private static volatile LazyThreadSafeDoubleCheck INSTANCE;

    private LazyThreadSafeDoubleCheck(){}

    public static LazyThreadSafeDoubleCheck getInstance() {

        if (INSTANCE == null) {
            synchronized (LazyThreadSafeDoubleCheck.class) {
                if (INSTANCE == null) {
                    // 初始化时分为实例化和赋值两步,尽管我们把这一步写成下面的语句
                    // 但Java虚拟机并不保证其他线程『眼中』这两步的顺序究竟是怎么样的
                    INSTANCE = new LazyThreadSafeDoubleCheck();
                }
            }
        }

        return INSTANCE;

    }

}